package com.itchen.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.itchen.hospital.common.Result;
import com.itchen.hospital.pojo.User;
import com.itchen.hospital.pojo.Params;
import com.itchen.hospital.service.UserService;
import com.itchen.hospital.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@Tag(name = "用户信息接口")
@CrossOrigin
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/getAll")
    @Operation(summary = "查询所有")
    public Result findByAll() {
        List<User> list = userService.getAll();
        return Result.success(list);
    }

    @GetMapping("/findBySearch")
    @Operation(summary = "条件查询")
    public Result findBySearch(Params params) {
        PageInfo<User> list = userService.search(params);
        return Result.success(list);
    }

    @PostMapping("/add")
    @Operation(summary = "添加用户")
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.success();
    }

    //根据account回显
    @GetMapping("/findByAccount")
    @Operation(summary = "根据account回显")
    public Result findByUserInfo(String account) {
        User user = userService.findByAccount(account);
        return Result.success(user);
    }

    @PostMapping("/update")
    @Operation(summary = "修改用户")
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.success();
    }

    @GetMapping("/delete")
    @Operation(summary = "删除用户")
    public Result delete(String account) {
        userService.delete(account);
        return Result.success();
    }

    @PostMapping("/batchDelete")
    @Operation(summary = "批量删除")
    public Result batchDelete(@RequestBody List<User> users) {
        userService.batchDelete(users);
        return Result.success();
    }

}
